//
//  ViewController.m
//  传感器
//
//  Created by student on 16/4/5.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *accelerInfoLabel;
@property (weak, nonatomic) IBOutlet UILabel *gyroInfoLabel;
//运动管理器
@property(nonatomic,strong)CMMotionManager *motionManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 检测运动（不需要知道xyz的具体加速度数值）比如摇一摇的功能
- (void) encodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"%s",__func__);
}
- (void) decodeRestorableStateWithCoder:(NSCoder *)coder
{
     NSLog(@"%s",__func__);
}
- (void) applicationFinishedRestoringState
{
     NSLog(@"%s",__func__);
}

//-(void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    if(self.motionManager.accelerometerAvailable)
//    {
//        //更新频率
//        self.motionManager.accelerometerUpdateInterval = 0.1;
//        //开始监听
//        [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
//            NSString *info = [NSString stringWithFormat:@"加速计信息:\nX:%+0.2f\nY:%+0.2f\nZ:%+0.2f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z];
//            self.accelerInfoLabel.text = info;
//        }];
//    }
//    else
//    {
//        self.accelerInfoLabel.text = @"当前设备加速计不可使用";
//    }
//    if(self.motionManager.gyroAvailable)
//    {
//        self.motionManager.gyroUpdateInterval = 0.1;
//        [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
//            NSString *info = [NSString stringWithFormat:@"回转信息:\nX:%+0.2f\nY:%+0.2f\nZ:%+0.2f",gyroData.rotationRate.x,gyroData.rotationRate.y,gyroData.rotationRate.z];
//            self.accelerInfoLabel.text = info;
//        }];
//    }
//}
//-(void)viewDidDisappear:(BOOL)animated
//{
//    [super viewDidDisappear:animated];
//    
//    //停止
//    [self.motionManager stopAccelerometerUpdates];
//    [self.motionManager stopGyroUpdates];
//    
//}
#pragma mark - 距离传感器
//-(void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    //开启传感器
//    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
//    //监听聚力传感器状态改变的通知 UIDeviceProximityStateDidChangeNotification
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(proximityStateChanged:) name:UIDeviceProximityStateDidChangeNotification object:nil];
//}
//-(void)viewDidDisappear:(BOOL)animated
//{
//    [super viewDidDisappear:animated];
//    [UIDevice currentDevice].proximityMonitoringEnabled = NO;
//    [[NSNotificationCenter defaultCenter]removeObserver:self];
//    
//}
//-(void)proximityStateChanged:(NSNotification *)t
//{
//    if([UIDevice currentDevice].proximityState == YES)
//    {
//        NSLog(@"有物体靠近");
//    }
//    else
//    {
//        NSLog(@"远离了");
//    }
//}
@end
